<?
require 'global.php';

$gee=$product;
$url='search';

$s=isset($_GET["s"])?trim(clean($_GET["s"])):'';
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"]; // 当前页码 current page
$sort=1; if(isset($_GET["sort"]))if(is_numeric($_GET["sort"]))$sort=$_GET["sort"];
$type=1; if(isset($_GET["type"]))if(is_numeric($_GET["type"]))$type=$_GET["type"];

switch ($sort) {
case 1:
	$order_by='id';
	$order_direction='desc';
	break;
case 2:
	$order_by='price';
	$order_direction='asc';
	break;
case 3:
	$order_by='price';
	$order_direction='desc';
	break;
}
$gee->rows=12;
$news->rows=6;
$where=array();
$where_t_info = array();
if($s!='') {
	$where_t_info[]=array('name'=>'name_'.$lang,'val'=>$s,'opn'=>'like', 'and'=>'and');
    $tags_info = $tags->get_rows_advanced(1024, $where_t_info);
    $tags_arr = array();
    foreach($tags_info as $key => $val){
        $tags_arr[] = $val['id'];
    }

    $where_tags = array();
    $where_tags[] = array('name'=>'tag_id', 'val'=>$tags_arr, 'opn'=>'in', 'and'=>'and');
    $where_tags[] = array('name'=>'tags_table', 'val'=>$type, 'opn'=>'=', 'and'=>'and');
    $tag_list_info = $tag_list->get_rows_advanced(1024, $where_tags);
    $tag_list_arr = array();
    foreach($tag_list_info as $key_t => $val_t){
        $tag_list_arr[] = $val_t['p_id'];
    }
    if($type == 1){
        $where[]=array('name'=>'name_'.$lang,'val'=>$s,'opn'=>'like', 'and'=>'and');
        $where[]=array('name'=>'sku','val'=>$s,'opn'=>'like', 'and'=>'or');
    }elseif($type == 2){
        $where[]=array('name'=>'name','val'=>$s,'opn'=>'like', 'and'=>'and');
    }
    $where[]=array('name'=>'id','val'=>$tag_list_arr,'opn'=>'in', 'and'=>'or');

	$where2=array();
	$where2[]=array('name'=>'text','val'=>$s);
	$row=$keyword->get_rows(1,"text",$s);
	if(empty($row)) {
		$arr=array(
		'text'=>$s,
		'hit_count'=>1,
		);
		$keyword->insert($arr);
	}
	else {
		$arr=array(
		'hit_count'=>$row[0]['hit_count']+1,
		);
		$keyword->update($arr, 'id', $row[0]['id']);
	}
    if($type == 1){
        $pages=$gee->count_pages($where);
        $info=$gee->get_page($page,$where,$order_by,$order_direction);
    }elseif($type == 2){
        $pages=$news->count_pages($where);
        $info=$news->get_page($page,$where,$order_by,$order_direction);
    }

}else{
    if($_GET['tag_id'] != ''){
        $s = $_GET['tag_id'];
        $tags_info = $tags->get_row($_GET['tag_id']);
        $s = $tags_info['name_'.$lang];

        $where_tags = array();
        $where_tags[] = array('name'=>'tag_id', 'val'=>$tags_info['id'], 'opn'=>'=', 'and'=>'and');
        $where_tags[] = array('name'=>'tags_table', 'val'=>1, 'opn'=>'=', 'and'=>'and');
        $tag_list_info = $tag_list->get_rows_advanced(1024, $where_tags);
        $tag_list_arr = array();
        foreach($tag_list_info as $key_t => $val_t){
            $tag_list_arr[] = $val_t['p_id'];
        }
        $where[]=array('name'=>'id','val'=>$tag_list_arr,'opn'=>'in', 'and'=>'and');
        $where2=array();
        $where2[]=array('name'=>'text','val'=>$s);
        $row=$keyword->get_rows(1,"text",$s);
        if(empty($row)) {
            $arr=array(
                'text'=>$s,
                'hit_count'=>1,
            );
            $keyword->insert($arr);
        }
        else {
            $arr=array(
                'hit_count'=>$row[0]['hit_count']+1,
            );
            $keyword->update($arr, 'id', $row[0]['id']);
        }
    }
    $pages=$gee->count_pages($where);
    $info=$gee->get_page($page,$where,$order_by,$order_direction);
//    if($type == 1){
}




// 热销产品 hot_products
$tem = $product->get_rows(32,'is_enabled',1,'sold_quantity','desc');
shuffle($tem);
$arr_hot=array();
$k=0;
foreach($tem as $row) { $arr_hot[]=$row; $k++; if($k==MAX_HOT_PRODUCT_SHOWN_SIDE) break; }

//  历史记录 history
$history=get_history();
$has_history=count($history);
$where_history=array();
$where_history[]=array('name'=>'is_enabled','val'=>1);
$where_history[]=array('name'=>'id','val'=>$history,'opn'=>'in');
$tem = $product->get_rows_advanced(32,$where_history);
//shuffle($tem);
$arr_history=array();
$k=0;
foreach($tem as $row) { $arr_history[]=$row; $k++; if($k==MAX_RECENTLY_VIEWED_PRODUCT_SHOWN_SIDE) break; }

// SEO 设置 seo tags
$title=WEB_SEARCHED_RESULT.' - '.$s;
$kwd=$title;
$dpn=$kwd;


function str2color($re, $str, $color){
    return str_replace($re, '<span style="color:'.$color.'">'.$re.'</span>', $str);
}

function tagsInfo($id, $tags_table, $s){
    global $tags, $tag_list, $lang;
    $where_t = array();
    $where_t[] = array('name'=>'p_id', 'val'=>$id, 'opn'=>'=', 'and'=>'and');
    $where_t[] = array('name'=>'tags_table', 'val'=>$tags_table, 'opn'=>'=', 'and'=>'and');
    $tags_list_info = $tag_list->get_rows_advanced(1024, $where_t);
    $tags_list_arr = array();
    foreach($tags_list_info as $val){
        $tags_list_arr[] = $val['tag_id'];
    }
    $where_d = array();
    $where_d[] = array('name'=>'id', 'val'=>$tags_list_arr, 'opn'=>'in', 'and'=>'and');
    unset($tags_list_info);
    $tags_list_info = $tags->get_rows_advanced(1024, $where_d);
    unset($tags_list_arr);
    $i = 0;
    foreach($tags_list_info as $val){
        if(stristr($val['name_'.$lang], $s)){
            $tags_list_arr[] = $val;
            $i++;
            break;
        }
    }
    foreach($tags_list_info as $val){
        if($val['name_'.$lang] != $tags_list_arr[0]['name_'.$lang]){
            $tags_list_arr[] = $val;
            if($i >= 2) break;
            $i++;
        }
    }
    return $tags_list_arr;
}

function str2sub($str, $num=50, $ishtml=0){
    if($ishtml){
        $str = trim($str); //清除字符串两边的空格
        $str = strip_tags($str); //利用php自带的函数清除html格式。保留P标签
        $str = preg_replace("/\t/","",$str); //使用正则表达式匹配需要替换的内容，如：空格，换行，并将替换为空。
        $str = preg_replace("/\r\n/","",$str);
        $str = preg_replace("/\r/","",$str);
        $str = preg_replace("/\n/","",$str);
//        $str = preg_replace("/ /","",$str);
//        $str = preg_replace("/  /","",$str);  //匹配html中的空格
        $str = str_replace('<.*?>', '', $str);
    }

    $len = mb_strlen($str, 'utf-8');
    $re_len = 0;
    $re_len_old = $num;
    $len_old = mb_strlen($str, 'utf-8');
    $new_str = '';
    for($i = 0; $i < $len_old; $i++){
        $new_str_i = mb_substr($str, $i, 1, 'utf-8');
        if(strlen($new_str_i)== 3){
            $re_len_old -= 2;
        }else{
            $re_len_old -= 1;
        }
        $new_str.=$new_str_i;
        if($re_len_old<=0) break;
    }

    return '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$new_str.'……';

}

// view 加载视图
require VIEW.$url.'.php';

// end of file
